SET allow_experimental_qbit_type = 1;


{% for i in range(0, 2) -%}

SET optimize_qbit_distance_function_reads = {{ i }};
SELECT '========== optimize_qbit_distance_function_reads = {{ i }} ==========';

-- Normal tests
SELECT 'Test L2DistanceTransposed: BFloat16';
DROP TABLE IF EXISTS qbit;
DROP TABLE IF EXISTS qbit_2;
CREATE TABLE qbit (id UInt32, vec QBit(BFloat16, 3)) ENGINE = Memory;
CREATE TABLE qbit_2 (id UInt32, vec QBit(BFloat16, 18)) ENGINE = Memory;

INSERT INTO qbit VALUES (1, [0,1,2]);
INSERT INTO qbit VALUES (2, [1,2,3]);
INSERT INTO qbit VALUES (3, [2,3,4]);
INSERT INTO qbit_2 VALUES (1, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]);
INSERT INTO qbit_2 VALUES (2, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]);
INSERT INTO qbit_2 VALUES (3, [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]);

-- Use rounding to avoid minor precision differences between different architectures
WITH [toBFloat16(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit ORDER BY id;
WITH [toBFloat16(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 8), 5) AS dist FROM qbit ORDER BY id;
WITH [toBFloat16(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 4), 5) AS dist FROM qbit ORDER BY id;
WITH [toBFloat16(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toBFloat16(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 8), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toBFloat16(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 4), 5) AS dist FROM qbit_2 ORDER BY id;

DROP TABLE qbit;
DROP TABLE qbit_2;


SELECT 'Test L2DistanceTransposed: Float32';
CREATE TABLE qbit (id UInt32, vec QBit(Float32, 3)) ENGINE = Memory;
CREATE TABLE qbit_2 (id UInt32, vec QBit(Float32, 18)) ENGINE = Memory;

INSERT INTO qbit VALUES (1, [0,1,2]);
INSERT INTO qbit VALUES (2, [1,2,3]);
INSERT INTO qbit VALUES (3, [2,3,4]);
INSERT INTO qbit_2 VALUES (1, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]);
INSERT INTO qbit_2 VALUES (2, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]);
INSERT INTO qbit_2 VALUES (3, [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]);

-- Use rounding to avoid minor precision differences when using non-vectorized implementations of distance function
WITH [toFloat32(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 32), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat32(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat32(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 8), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 32), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toFloat32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toFloat32(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 8), 5) AS dist FROM qbit_2 ORDER BY id;

DROP TABLE qbit;
DROP TABLE qbit_2;


SELECT 'Test L2DistanceTransposed: Float64';
CREATE TABLE qbit (id UInt32, vec QBit(Float64, 3)) ENGINE = Memory;
CREATE TABLE qbit_2 (id UInt32, vec QBit(Float64, 18)) ENGINE = Memory;


INSERT INTO qbit VALUES (1, [0,1,2]);
INSERT INTO qbit VALUES (2, [1,2,3]);
INSERT INTO qbit VALUES (3, [2,3,4]);
INSERT INTO qbit_2 VALUES (1, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]);
INSERT INTO qbit_2 VALUES (2, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]);
INSERT INTO qbit_2 VALUES (3, [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]);

WITH [toFloat64(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 64), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat64(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 32), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat64(0), 1, 2] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit ORDER BY id;
WITH [toFloat64(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 64), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toFloat64(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 32), 5) AS dist FROM qbit_2 ORDER BY id;
WITH [toFloat64(0), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] AS reference_vec SELECT id, vec, round(L2DistanceTransposed(vec, reference_vec, 16), 5) AS dist FROM qbit_2 ORDER BY id;

DROP TABLE qbit;
DROP TABLE qbit_2;

{% endfor -%}
